package cn.com.connected.controller.open;

import cn.com.connected.base.exception.BusinessException;
import cn.com.connected.base.exception.ErrorCode;
import cn.com.connected.common.config.oss.OssClient;
import cn.com.connected.common.emnus.FileType;
import cn.com.connected.vo.base.PolicyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


/**
 * 权限
 * @author sms
 * @email sms@live.com
 * @date 2023-05-11 22:55:15
 */
@Api(tags = "OSS")
@RestController
@Slf4j
public class OssController {

    @Resource
    private OssClient ossClient;

    @ApiOperation(value="描述: 后端转传  ok")
    @RequestMapping(value="/open/v1/oss/upload/{fileType}",method= RequestMethod.POST)
    public void upload(@PathVariable("fileType") Integer fileType, @RequestParam("file") MultipartFile multipartFile){
        try {
            ossClient.uploadOpenBucketName(FileType.createOssKey(fileType),multipartFile.getInputStream());
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_FAIL,"上传失败");
        }
    }

    @ApiOperation(value="描述: 前端直传 获取授权地址 ok")
    @RequestMapping(value="/open/v1/oss/authorize/{fileType}",method= RequestMethod.GET)
    public PolicyVO authorize(@PathVariable("fileType") Integer fileType) throws UnsupportedEncodingException {
        return ossClient.createPolicy(FileType.createOssKey(fileType));
    }


}
